package com.krtech.wecard.module.sys.service.impl;

import cn.hutool.json.JSONUtil;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.krtech.wecard.module.sys.mapper.SysUserOperateLogMapper;
import com.krtech.wecard.module.sys.entity.SysUserOperateLog;
import com.krtech.wecard.module.sys.service.SysUserOperateLogService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysUserOperateLogServiceImpl extends ServiceImpl<SysUserOperateLogMapper, SysUserOperateLog> implements SysUserOperateLogService {

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public int updateBatch(List<SysUserOperateLog> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<SysUserOperateLog> list) {
        return baseMapper.batchInsert(list);
    }

    /**
     * 提供更新后的数据
     *
     * @param sysUser
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUserLog(SysUser sysUser) {
        SysUser userInfo = LoginUtil.getUserInfo();

        //增加用户变更日志
        SysUser fromUserInfo = sysUserMapper.selectById(sysUser.getUserId());

        SysUserOperateLog sysUserOperateLog = new SysUserOperateLog();
        sysUserOperateLog.setCreatedBy(userInfo.getUserId().toString());
        sysUserOperateLog.setCreatedTime(new Date());
        sysUserOperateLog.setFromInfo(JSONUtil.toJsonStr(fromUserInfo));
        sysUserOperateLog.setToInfo(JSONUtil.toJsonStr(sysUser));

        sysUserOperateLog.setUserId(fromUserInfo.getUserId().intValue());
        sysUserOperateLog.setUserCode(fromUserInfo.getUserCode());
        sysUserOperateLog.setNickName(fromUserInfo.getNickName());

        boolean save = this.save(sysUserOperateLog);

        return save;
    }
}


